<script setup lang="ts">
import { computed } from 'vue'
import { useChatStore } from '@/stores/chat'
import MessageList from './MessageList.vue'

const chatStore = useChatStore()

// 计算属性：连接状态显示文本
const connectionStatus = computed(() => {
  return chatStore.connected ? '已连接' : '已断开'
})

// 计算属性：连接状态样式类
const statusClass = computed(() => {
  return {
    'status-connected': chatStore.connected,
    'status-disconnected': !chatStore.connected,
  }
})
</script>

<template>
  <div class="chat-window">
    <div class="chat-header">
      <div class="header-content">
        <h2>📱 在线聊天室</h2>
        <div class="status-indicator" :class="statusClass">
          <span class="status-dot"></span>
          {{ connectionStatus }}
        </div>
      </div>
    </div>

    <div class="user-info">
      <span class="user-icon">👤</span>
      <span class="user-label">当前用户:</span>
      <strong class="username">{{ chatStore.currentUser }}</strong>
    </div>

    <MessageList />

    <div v-if="chatStore.error" class="error-message">
      <span class="error-icon">⚠️</span>
      {{ chatStore.error.message }}
    </div>
  </div>
</template>

<style scoped>
.chat-window {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f5f7fa;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  font-family: 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
}

.chat-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 16px 24px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.chat-header h2 {
  margin: 0;
  font-size: 1.4rem;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.status-indicator {
  display: flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 500;
  backdrop-filter: blur(5px);
  background-color: rgba(255, 255, 255, 0.15);
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}

.status-connected .status-dot {
  background-color: #4ade80;
  box-shadow: 0 0 8px #4ade80;
}

.status-disconnected .status-dot {
  background-color: #f87171;
  box-shadow: 0 0 8px #f87171;
}

.user-info {
  padding: 12px 24px;
  background-color: white;
  font-size: 0.95rem;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-icon {
  font-size: 1.1rem;
}

.user-label {
  color: #64748b;
}

.username {
  color: #334155;
  font-weight: 600;
}

.error-message {
  padding: 12px 24px;
  background-color: #fee2e2;
  color: #b91c1c;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 8px;
  border-top: 1px solid #fecaca;
}

.error-icon {
  font-size: 1.1rem;
}

@media (max-width: 768px) {
  .chat-header {
    padding: 12px 16px;
  }

  .chat-header h2 {
    font-size: 1.2rem;
  }

  .user-info,
  .error-message {
    padding: 10px 16px;
  }
}
</style>
